import $api from '@/http/api.js'

export default{
    namespaced:true,//为了使其属性作用于局部
    state:{
        rowsData:[],
        oneData:{},
        imgData:{},
    },
    mutations:{
        //获取轮播图
        GET_DATA(state,payload){
            // console.log(payload);
            state.rowsData=payload
        },
        GET_ONE_DATA(state,payload){
            // console.log('payload',payload);
            state.oneData=payload
        },
        GET_IMG_DATA(state,payload){
            const res = payload.map(item => item.image_src.split('/')[4])
            // console.log('res',res);
            state.imgData =res
        }
    },
    actions:{
        //获取轮播图
        async getDataAsync(context) {
            const {data} = await $api.swiper.getAllSwiper();
            // console.log('获取轮播图',data);
            if (data.meta.status == 200) {
                context.commit('GET_DATA', data.message)
                context.commit('GET_IMG_DATA', data.message)
            }
        },
        //获取一张轮播图
        async getOneDataAsync(context,_id) {
            const {data} = await $api.swiper.findByIdSwiper({_id});
            // console.log('获取一张轮播图',data);
            if (data.code == 200) {
                context.commit('GET_ONE_DATA', data.data)
            }
        },
        //删除轮播图
        async deleteDataAsync(context,_id) {
            // console.log('_id',_id);
            const { data } = await $api.swiper.deleteSwiper({_id});
            return data
        },
        //新增轮播图
        async addDataAsync(context,swiperOne){
            // console.log('轮播图',swiperOne);
            return await  $api.swiper.addSwiper(swiperOne)
           
        },
        //修改轮播图
        async updateAsync(context,swiperOne){
            // console.log('轮播图',swiperOne);
            return await $api.swiper.updateSwiper(swiperOne)
        },
        //轮播图上下架
        async updateSwiperShow(context,_id){
            // console.log('轮播图上下架',_id);
            return await  $api.swiper.updateSwiperShow({_id})
           
        },
        //删除轮播图图片
        async deleteImgAsync(context,file) {
            console.log('file',file);
            return await $api.swiper.deleteFiles(file);
        },
    }
}